<?php

namespace app\controller\web;

use app\service\AdminAcountService;
use app\service\AdminPathinfoService;
use app\validate\AdminAccountValidate;
use app\validate\PageValidate;
use share\service\ContextService;

class AdminAccountController extends WebBaseController{

    public function initValidate(){
        $this->validate = new AdminAccountValidate();
    }

    /**
     * 获取当前管理员最新信息
     */
    public function user_info_action(){
        $userInfo = ContextService::getUserInfo();
        if(!isset($userInfo['vendorName'])){
            $userInfo['vendorName'] = $userInfo['vendor'];
        }
        $userInfo['token'] = $this->request->header('token');
        $roleInfo = AdminPathinfoService::getRoleInfoByUser($userInfo['id']);
        return self::ok([
            'userInfo' => $userInfo,
            'roleInfo' => $roleInfo,
        ]);
    }

    /**
     * 获取管理员列表
     */
    public function list_action(){
        $page = $this->check('pageInfo',null,(new PageValidate()));
        $data = AdminAcountService::getListInfo(null,$page);
        $total = AdminAcountService::getListCountInfo(null);
        return self::ok(self::formatPageData($page,$total,$data));
    }

    /**
     * 新增管理员
     */
    public function add_action(){
        $input = $this->check();
        $res = AdminAcountService::addAdmin($input);
        return self::ok($res);
    }


    /**
     * 修改管理员
     */
    public function edit_action(){
        $input = $this->check();
        $res = AdminAcountService::updateAdmin($input);
        return self::ok($res); 
    }

    /**
     * 删除管理员
     */
    public function del_action(){
        $input = $this->check();
        $res = AdminAcountService::delAdmin($input['id']);
        return self::ok($res);   
    }

    /**
     * 修改密码
     */
    public function change_password_action(){
        $input = $this->check();
        $res = AdminAcountService::updatePassword($input['id'],$input['password'],$input['oldPwd']);
        return self::ok($res);   
    }
}